﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Xml;
using DiscoveryLogic.Common.Utilities;

namespace DiscoveryLogic.Analytics.DataObjects
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public abstract class EntityBase
    {
        private readonly EntityType _EntityType;
        /// <summary></summary>
        public EntityType EntityType
        {
            get
            {
                return this._EntityType;
            }
        }

        private readonly string _IdField;
        public string IDField
        {
            get
            {
                return _IdField;
            }
        }

        private readonly string _NameField;
        public string NameField
        {
            get
            {
                return _NameField;
            }
        }

        private int _CollapsedID;
        /// <summary></summary>
        public int CollapsedID
        {
            get
            {
                return this._CollapsedID;
            }
            set
            {
                this._CollapsedID = value;
            }
        }

        private double _Score;
        /// <summary>
        /// this is a weighted sum that can be used to represent 
        /// vertice size that can not be easily derived from 
        /// graph metrics
        /// </summary>
        public double Score
        {
            get
            {
                return this._Score;
            }
            set
            {
                this._Score = value;
            }
        }

        private string _Flag;
        /// <summary>
        /// this field is used to classify entities of the same type
        /// i.e. authors who published papers on dengue virus and 
        /// authors who published papers on dengue vaccines
        /// </summary>
        public string Flag
        {
            get
            {
                return this._Flag;
            }
            set
            {
                this._Flag = value;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="entityType"></param>
        /// <param name="idField"></param>
        /// <param name="nameField"></param>
        public EntityBase(EntityType entityType, string idField, string nameField)
        {
            this._EntityType = entityType;
            this._IdField = idField;
            this._NameField = nameField;
            this._Score = 1.0;
            this._Flag = string.Empty;
        }

        /// <summary>
        /// 
        /// </summary>
        public string PKValue()
        {
            PropertyInfo pkProp = this.GetType().GetProperty(this._IdField);
            return pkProp.GetValue(this, null).ToString();
        }

        /// <summary>
        /// 
        /// </summary>
        public string Name
        {
            get
            {
                if (!string.IsNullOrEmpty(this._NameField))
                {
                    PropertyInfo nameProp = this.GetType().GetProperty(this._NameField);
                    object propValue = nameProp.GetValue(this, null);
                    if (propValue != null)
                        return propValue.ToString();
                    else
                        return string.Empty;
                }
                else
                    return null;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="propName"></param>
        /// <returns></returns>
        public object GetPropertyValue(string propName)
        {
            PropertyInfo prop = this.GetType().GetProperty(propName);
            if (prop != null)
                return prop.GetValue(this, null);
            else
                return null;
        }

        public static PropertyInfo[] GetProperties(EntityType entityType)
        {
            PropertyInfo[] props = EntityTypeUtil.GetProperties(entityType);
            List<PropertyInfo> propList=new List<PropertyInfo>();
            foreach(PropertyInfo prop in props)
            {
                if(prop.CanRead && prop.CanWrite)
                    propList.Add(prop);
            }
            return propList.ToArray();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="xDoc"></param>
        /// <param name="entityNode"></param>
        public abstract void Save(ref XmlDocument xDoc, XmlNode entityNode);
    }
}
